package cn.sdhuijin.vms.websocket;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("deprecation")
public class MyMessageInbound extends MessageInbound {
	final static Logger logger = LoggerFactory
			.getLogger(MyMessageInboundPool.class);
	private String org;
	private Boolean first = Boolean.valueOf(true);

	public String getOrg() {
		return this.org;
	}

	protected void onOpen(WsOutbound outbound) {
		String message = "有websock连接";
		logger.info("onOpen: " + message);
	}

	protected void onClose(int status) {
		MyMessageInboundPool.remove(this);
		String message = "关闭" + getOrg();
		MyMessageInboundPool.broadcast(CharBuffer.wrap(message));
		logger.info("onClose: " + message);
	}

	protected void onBinaryMessage(ByteBuffer message) throws IOException {
	}

	protected void onTextMessage(CharBuffer message) throws IOException {
		String filteredMessage = message.toString();
		if (this.first.booleanValue()) {
			this.org = filteredMessage;
			MyMessageInboundPool.add(this);
			logger.info("onTextMessage: " + this.org);
			this.first = Boolean.valueOf(false);
		} else {
			logger.info("onTextMessage: " + this.org + "-" + filteredMessage);
			MyMessageInboundPool.sendMessage("济宁公安消防支队",
					CharBuffer.wrap(filteredMessage));
		}
	}
}